package com.blockmeta.bbs.minemodule.activity;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blockmeta.bbs.baselibrary.provider.IApplicationProvider;
import com.blockmeta.bbs.baselibrary.utils.CacheUtils;
import com.blockmeta.bbs.baselibrary.utils.SharedPreferenceUtil;
import com.blockmeta.bbs.baselibrary.utils.ToastUtil;
import com.blockmeta.bbs.businesslibrary.arouter.RouterHelper;
import com.blockmeta.bbs.businesslibrary.arouter.RouterPath;
import com.blockmeta.bbs.businesslibrary.base.BaseActivity;
import com.blockmeta.bbs.businesslibrary.business.UmengEvent;
import com.blockmeta.bbs.businesslibrary.constant.Constant;
import com.blockmeta.bbs.businesslibrary.receiver.ApkInstallReceiver;
import com.blockmeta.bbs.businesslibrary.util.DialogUtil;
import com.blockmeta.bbs.minemodule.BuildConfig;
import com.blockmeta.bbs.minemodule.R;

/**
 * Created by author on 2018/1/4.
 */

public class SettingActivity extends BaseActivity implements View.OnClickListener ,DialogUtil.OnDialogClickListener{
    private Context mContext;
    private View v;
    private TextView cache,updateHint;
    private RelativeLayout click1;
    private RelativeLayout click3;

    @Override
    protected boolean showTitle() {
        return true;
    }

    @Override
    public View setView() {
        mContext = SettingActivity.this;
        setTitleText("设置");
//        setStatusBarColor(R.color.status_red);
        v = LayoutInflater.from(mContext).inflate(R.layout.activity_setting, null);
        initData();
        return v;
    }

    private void initData() {
        click1 = (RelativeLayout) v.findViewById(R.id.setting_click1);
        click3 = (RelativeLayout) v.findViewById(R.id.setting_click3);
        click1.setOnClickListener(this);
        click3.setOnClickListener(this);
        RelativeLayout relativeLayout= (RelativeLayout) v.findViewById(R.id.setting_check_update);
        relativeLayout.setOnClickListener(this);
        updateHint= (TextView) v.findViewById(R.id.tv_check_update);

        cache = (TextView) v.findViewById(R.id.setting_cache);
        try {
            cache.setText(CacheUtils.getTotalCacheSize(mContext));
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (SharedPreferenceUtil.getString(this, Constant.APP_VERSION,null)==null){
            updateHint.setText("暂无更新");
            return;
        }
        IApplicationProvider provider = RouterHelper.getProvider(RouterPath.APPLICATION_PROVIDER);
        String[] local= provider.getProjectBuildInfo().version_name.split("\\.");
        String[] remote=SharedPreferenceUtil.getString(this,Constant.APP_VERSION,"").split("\\.");

        if (getInt(local[0])<getInt(remote[0])){
            updateHint.setText("发现新版本");
        }else if (getInt(local[0])==getInt(remote[0])&&getInt(local[1])<getInt(remote[1])){
            updateHint.setText("发现新版本");
        }else if (getInt(local[0])==getInt(remote[0])&&getInt(local[1])==getInt(remote[1])&&getInt(local[2])<getInt(remote[2])){
            updateHint.setText("发现新版本");
        }else {
            updateHint.setText("暂无更新");
        }
    }

    @Override
    public void onClick(View view) {
        int i = view.getId();
        if (i == R.id.setting_click1) {
            CacheUtils.clearAllCache(mContext);
            try {
                cache.setText(CacheUtils.getTotalCacheSize(mContext));
            } catch (Exception e) {
                e.printStackTrace();
            }
            UmengEvent.eventStatistics(mContext, Constant.U_MINE_SETTING_CLEARCACHE, null);

        } else if (i == R.id.setting_click3) {
            activityStartActivity(mContext, AboutUsActivity.class);
            UmengEvent.eventStatistics(mContext, Constant.U_MINE_SETTING_ABOUTUS, null);

        } else if (i == R.id.setting_check_update) {
            checkUpdate();
        }
    }

    //检查更新
    private void checkUpdate() {
        if (SharedPreferenceUtil.getString(this,Constant.APP_VERSION,null)==null)return;
        IApplicationProvider provider = RouterHelper.getProvider(RouterPath.APPLICATION_PROVIDER);
        String[] local=provider.getProjectBuildInfo().version_name.split("\\.");
        String[] remote=SharedPreferenceUtil.getString(this,Constant.APP_VERSION,"").split("\\.");
        boolean force = SharedPreferenceUtil.getBoolean(this,Constant.ISFORCE,false);
        if (getInt(local[0])<getInt(remote[0])){
            showUpdate(force);
        }else if (getInt(local[0])==getInt(remote[0])&&getInt(local[1])<getInt(remote[1])){
            showUpdate(force);
        }else if (getInt(local[0])==getInt(remote[0])&&getInt(local[1])==getInt(remote[1])&&getInt(local[2])<getInt(remote[2])){
            showUpdate(force);
        }else {
            ToastUtil.show("您已是最新版本");
        }
    }
    private void showUpdate(boolean force){
        if (force){
            //强制更新
            DialogUtil.showMustUpdate(this,this);
        }else {
            DialogUtil.showUpdate(this,this);
        }
    }
    private int getInt(String a){
        return Integer.valueOf(a);
    }

    @Override
    public void onDialogClick(int id) {
        String url = SharedPreferenceUtil.getString(this,Constant.UPDATE_URL,null);
        if (url!=null)
            updateApp(url);
    }
    /**
     * 下载更新
     * @param url 下载链接
     */
    private void updateApp(String url) {
        ApkInstallReceiver.downloadApkFile(SettingActivity.this,null);
    }

}
